//
//  RegistrationTypeController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/4/20.
//  Copyright © 2020 QDSG. All rights reserved.
//

import UIKit
/// 挂号-门诊选择
class RegistrationTypeController: BaseViewController {
    
    var viewModelLeft: DepartmentViewModel? {
        didSet {
            tableViewLeft.reloadData()
        }
    }
    var viewModelRight: DepartmentViewModel? {
        didSet {
            tableViewRight.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setSearchBar()
        setUI()
        loadDepartmentLevelOne(name: "", parentId: "0", level: "0")
        // Do any additional setup after loading the view.
    }
    
    func setUI() {
        tableViewLeft.tableFooterView = UIView()
        tableViewLeft.rowHeight = 55
        tableViewLeft.register(RegistrationTypeCell.nibForCell(),
                               forCellReuseIdentifier: RegistrationTypeCell.reuseIdentifier())
        
        tableViewRight.tableFooterView = UIView()
        tableViewRight.rowHeight = 55
        tableViewRight.register(RegistrationTypeListCell.nibForCell(),
                                forCellReuseIdentifier: RegistrationTypeListCell.reuseIdentifier())
    }
    
    func setSearchBar() {
        let titleView = UINib.loadViewFrom(YSGSearchTitleView.self)
        titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 30)
        navigationItem.titleView = titleView
        titleView.handlerClosure = { [weak self] _ in
            self?.presentSearchVc()
        }
    }
    
    private func presentSearchVc() {
        let cloudVc = DateReserveController(with: .none)
        navigationController?.pushViewController(cloudVc, animated: true)
    }

    @IBOutlet weak var tableViewLeft: UITableView!
    @IBOutlet weak var tableViewRight: UITableView!
    @IBOutlet weak var headerNoticView: RegistrationNoticeView! {
        didSet {
            headerNoticView.tapClosure = {
                let agreementView = UINib.loadViewFrom(AgreementAlertView.self)
                agreementView.show(type: .registrationRule)
            }
        }
    }
    
}

//tag100:tableViewLeft tag101:tableViewRight
extension RegistrationTypeController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView.tag == 100 {
            return viewModelLeft?.numberOfRows ?? 0
        }
        if tableView.tag == 101 {
            return viewModelRight?.numberOfRows ?? 0
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView.tag == 100 {
            let cell = tableView.dequeueReusableCell(withIdentifier: RegistrationTypeCell.reuseIdentifier(), for: indexPath) as! RegistrationTypeCell
            if let vm = viewModelLeft?.viewModelForRegistration(for: indexPath) {
                cell.updateData(vm: vm)
                tableViewLeft.selectRow(at: IndexPath(row: 0, section: 0), animated: true, scrollPosition: .top)
            }
            return cell
        }
        if tableView.tag == 101 {
            let cell = tableView.dequeueReusableCell(withIdentifier: RegistrationTypeListCell.reuseIdentifier(), for: indexPath) as! RegistrationTypeListCell
            if let vm = viewModelRight?.viewModelForRegistration(for: indexPath) {
                cell.updateData(vm: vm)
            }
            return cell
        }
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if tableView.tag == 100 {
            if let rowVm = viewModelLeft?.viewModelForRegistration(for: indexPath) {
                loadDepartmentLevelTwo(name: "", parentId: rowVm.departmentId, level: "1")
            }
        }
        if tableView.tag == 101 {
            if let rowVm = viewModelRight?.viewModelForRegistration(for: indexPath) {
                let cloudVc = DateReserveController(with: .none)
                cloudVc.departmentId = rowVm.departmentId
                cloudVc.isFromRegistrationType = true
                cloudVc.title = rowVm.departmentName
                navigationController?.pushViewController(cloudVc, animated: true)
            }
        }
    }
}

extension RegistrationTypeController {
    func loadDepartmentLevelOne(name: String, parentId: String, level: String) {
        DepartmentViewModel.getDepartmentList(parentId: parentId, level: level, success: { (models) in
            let vm = DepartmentViewModel(models: models)
            guard vm.numberOfRows > 0 else { return }
            self.viewModelLeft = vm
            let rowVm = vm.viewModelForRegistration(for: IndexPath(row: 0, section: 0))
            self.loadDepartmentLevelTwo(name: "", parentId: rowVm.departmentId, level: "1")
        }) { (errorMsg) in
            HUDTool.showError(message: errorMsg)
        }
    }
    
    func loadDepartmentLevelTwo(name: String, parentId: String, level: String) {
        DepartmentViewModel.getDepartmentList(parentId: parentId, level: level, success: { (models) in
            self.viewModelRight = DepartmentViewModel(models: models)
        }) { (errorMsg) in
            HUDTool.showError(message: errorMsg)
        }
    }
}
